import { createRouter, createWebHashHistory } from 'vue-router'
import { useUserStore } from '@/stores/userStore/user'

const Cesium = () => import('@/views/CesiumView.vue')
//
const Login = () => import('@/views/login/index.vue')
const RealLogin = () => import('@/views/login/components/login.vue')
const Register = () => import('@/views/login/components/register.vue')
const ForgotPass = () => import('@/views/login/components/forgotpass.vue')
const User = () => import('@/views/user/index.vue')
//
const Jsc = () => import('@/views/left/jsc/index.vue')
const Jyjc = () => import('@/views/left/jyjc/index.vue')
const Mnxl = () => import('@/views/left/mnxl/index.vue')
const Tyfp = () => import('@/views/left/tyfp/index.vue')
const Dtfw = () => import('@/views/right/dtfw/index.vue')
//
const Wjcz = () => import('@/views/right/wjcz/index.vue')
const Sjgl = () => import('@/views/right/sjgl/index.vue')
const Rzgl = () => import('@/views/right/rzgl/index.vue')

const DynamicRoutes = [
  {
    path: '/',
    redirect: '/jsc',
    component: Cesium,
    children: [
      {
        path: 'jsc',
        name: 'jsc',
        pageName: '驾驶舱',
        component: Jsc,
        meta: {
          keepAlive: true,
        },
      },

      {
        path: 'jyjc',
        name: 'jyjc',
        pageName: '救援决策',
        component: Jyjc,
        meta: {
          keepAlive: false,
        },
      },

      {
        path: 'mnxl',
        name: 'mnxl',
        pageName: '模拟训练',
        component: Mnxl,
        meta: {
          keepAlive: false,
        },
      },
      {
        path: 'tyfp',
        name: 'tyfp',
        pageName: '推演复盘',
        component: Tyfp,
        meta: {
          keepAlive: false,
        },
      },

      {
        path: 'dtfw',
        name: 'dtfw',
        pageName: '地图服务',
        component: Dtfw,
        meta: {
          keepAlive: false,
        },
      },
      //
      {
        path: 'wjcz',
        name: 'wjcz',
        pageName: '文件操作',
        component: Wjcz,
        meta: {
          keepAlive: false,
        },
      },
      {
        path: 'sjgl',
        name: 'sjgl',
        pageName: '数据管理',
        component: Sjgl,
        meta: {
          keepAlive: false,
        },
      },
      {
        path: 'rzgl',
        name: 'rzgl',
        pageName: '日志管理',
        component: Rzgl,
        meta: {
          keepAlive: false,
        },
      },

      //
      {
        path: '/user',
        name: 'user',
        pageName: '系统设置',
        component: User,
        meta: {
          keepAlive: false,
        },
      },
    ],
  },
]
const StaticRoutes = [
  {
    path: '/logins',
    pageName: 'login',
    component: Login,
    children: [
      {
        path: '',
        name: 'login',
        pageName: '登录',
        component: RealLogin,
        meta: {
          keepAlive: false,
        },
      },
      {
        path: '/register',
        name: 'register',
        pageName: '注册',
        component: Register,
        meta: {
          keepAlive: false,
        },
      },
      {
        path: '/forgotpass',
        name: 'forgotpass',
        pageName: '忘记密码',
        component: ForgotPass,
        meta: {
          keepAlive: false,
        },
      },
    ],
  },
]

const routes = [...DynamicRoutes, ...StaticRoutes]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

const whiteList = ['/logins', '/register', '/forgotpass']
// 添加全局前置守卫
router.beforeEach((to, from, next) => {
  // 获取用户store
  const userStore = useUserStore()

  // 如果访问的是登录页面，直接放行
  if (whiteList.includes(to.path)) {
    next()
    return
  }

  // 检查用户是否已登录（通过检查token是否存在）
  if (userStore.getToken) {
    // 用户已登录，允许访问
    next()
  } else {
    // 用户未登录，重定向到登录页面
    next('/logins')
  }
})

export default router
